#include <bits/stdc++.h>

using namespace std;

class Solution
{
public:
    static bool cmp(vector<int> &a, vector<int> &b)
    {
        return a[0] < b[0];
    }
    vector<vector<int>> merge(vector<vector<int>> &intervals)
    {
        sort(intervals.begin(), intervals.end(), cmp);
        vector<vector<int>> res;
        int start = intervals[0][0];
        int end = intervals[0][1];
        for (size_t i = 1; i < intervals.size(); i++)
        {
            if (intervals[i][0] > end)
            {
                res.push_back(vector<int>{start, end});
                start = intervals[i][0];
            }
            end = max(end, intervals[i][1]);
        }
        res.push_back(vector<int>{start, end});
        return res;
    }
};

int main()
{
    vector<vector<int>> intervals{{1, 3},
                                  {2, 6},
                                  {8, 10},
                                  {15, 18}};
    Solution sol;
    vector<vector<int>> res = sol.merge(intervals);
    for (auto vec : res)
    {
        for (auto num : vec)
        {
            cout << num << " ";
        }
        cout << endl;
    }
    return 0;
}